﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Utility;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;

namespace Portal.Exam.Render
{
    public class AddFreeText 
    {
        [AjaxPro.AjaxMethod]
        public string GetHtml(string SiteId)
        {
            string MonthiId = (string)WebEnvironments.Request(SiteId, "MonthiId");
            string Type = (string)WebEnvironments.Request(SiteId, "Type");
            string BackUrl = "ExamQuestion.aspx";
            string RetHtml =
                " <script language=javascript> \r\n" +
                " function ClientSaveAddNewFreeTextQuestion() \r\n" +
                " { \r\n" +
                "     SiteId='" + SiteId + "';\r\n" +
                "     Type=document.getElementById(\"txtType\").value; \r\n" +
                "     Title=document.getElementById(\"txtQuestion\").value; \r\n" +
                "     AnswerTip=document.getElementById(\"txtAnswerTip\").value; \r\n" +
                "     MaxPoint=document.getElementById(\"txtPoint\").value; \r\n" +
                "     MonthiId=document.getElementById(\"txtMonthiId\").value; \r\n" +
                "     SecondTime = document.getElementById(\"txtSecondTime\").value; \r\n" +
                "     IsMemory = document.getElementById(\"chkIsMemory\").checked; \r\n" +
                "     SecondHidden = document.getElementById(\"txtSecondHidden\").value; \r\n" +
                "     QuestionId=Portal.Exam.Render.AddFreeText.ServerSideSaveAddNewFreeTextQuestion(SiteId, MonthiId,Type,Title,MaxPoint,AnswerTip,SecondTime,IsMemory,SecondHidden).value; \r\n" +
                "     if(QuestionId.indexOf(\"ERROR\")!=-1) \r\n" +
                "     { \r\n" +
                "         alert(QuestionId); \r\n" +
                "         return; \r\n" +
                "     } \r\n" +
                "     document.getElementById(\"txtQuestion\").value=\"\"; \r\n" +
                "     document.getElementById(\"txtPoint\").value=\"1\"; \r\n" +
                "     document.getElementById(\"txtSecondTime\").value=\"60\"; \r\n" +
                "     document.getElementById(\"txtQuestion\").focus(); \r\n" +
                "     ShowClientUpdateUrlForm(QuestionId); \r\n" +
                " } \r\n" +

                 " function ShowClientUpdateUrlForm(QuestionId) \r\n" +
                " { \r\n" +
                "     SiteId='" + SiteId + "';\r\n" +
                "     Url=Portal.Exam.Render.AddFreeText.GetShowUpdateForm(SiteId, QuestionId).value; \r\n" +
                "     if(Url.indexOf(\"ERROR\")!=-1) \r\n" +
                "     { \r\n" +
                "         alert(Url); \r\n" +
                "         return; \r\n" +
                "     } \r\n" +
                "     window.open(Url, '_self'); \r\n" +
                " } \r\n" +
                " </script>\r\n" +
            "   <input type=hidden id=txtMonthiId value=\"" + MonthiId + "\">" +
            "   <input type=hidden id=txtType value=\"" + Type + "\">" +
            "   <div class=FormSaveUpdate>\r\n" +
            "      <table>" +
            "          <tr>" +
            "              <td class=\"Col1\">Câu hỏi:</td>" +
            "              <td class=\"Col2\"><textarea class=cssTextBoxQuestion id=txtQuestion></textarea></td>" +
            "          </tr>" +
            "          <tr>" +
            "              <td class=\"Col1\">Điểm:</td>" +
            "              <td class=\"Col2\"><input id=txtPoint value=\"1\"></td>" +
            "          </tr>" +
            "          <tr>" +
            "              <td class=\"Col1\">Thời gian(giây):</td>" +
            "              <td class=\"Col2\"><input id=txtSecondTime value=\"30\"></td>" +
            "          </tr>" +
            "          <tr>" +
            "              <td class=\"Col1\">Gợi ý trả lời:</td>" +
            "              <td class=\"Col2\"><textarea class=cssTextBoxQuestion id=txtAnswerTip></textarea></td>" +
            "          </tr>" +
            "          <tr>" +
            "              <td class=\"Col1\">Câu hỏi trí nhớ:</td>" +
            "              <td class=\"Col2\"><input type=checkbox id=chkIsMemory></td>" +
            "          </tr>" +
            "          <tr>" +
            "              <td class=\"Col1\">Thời gian ẩn:</td>" +
            "              <td v><input id=txtSecondHidden value=\"30\"></td>" +
            "          </tr>" +
            "          <tr>" +
            "              <td></td>" +
            "              <td><input type=button  onclick=javascript:ClientSaveAddNewFreeTextQuestion(); value=\"Ghi lại\"><input type=button  onclick=\"javascript:window.history.back(-1);\" value=\"Quay lại\"><input type=button  onclick=javascript:window.open('" + BackUrl + "','_self'); value=\"Danh sách\"></td>" +
            "          </tr>" +
            "   </table>" +
            "</div>\r\n";
            return RetHtml;
        }

        public string Draw(string SiteId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                string RetHtml = "<div id=divAddBestAnsContent>"+ GetHtml(SiteId)+"</div>";

                return RetHtml;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }   

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveAddNewFreeTextQuestion(string SiteId, string MonthiId, string Type, string Title, string MaxPoint, string AnswerTip, string SecondTime, bool IsMemory, string SecondHidden)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                if (Title.Trim().Length == 0) throw new Exception("Chưa nhập tiêu đề");
                if (FunctionUtilities.checkInteger(MaxPoint) == false) throw new Exception("Dữ liệu điểm nhập không hợp lệ! Phải nhập kiểu số!");
                return new QuestionBll().AddFreeTextQuestion(null, SiteId, Type, MonthiId, Title, int.Parse(MaxPoint), AnswerTip, int.Parse(SecondTime),IsMemory,int.Parse(SecondHidden), 1);
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string SiteId,string QuestionId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                QuestionCls QuestionCls = new QuestionBll().CreateModel(SiteId, QuestionId);

                string UpdateUrl = "";
                if (QuestionCls.Type.ToLower().Equals("yesno"))
                {
                    UpdateUrl = "UpdateYesNo.aspx?QuestionId="+QuestionId;
                }
                if (QuestionCls.Type.ToLower().Equals("bestanswer"))
                {
                    UpdateUrl = "UpdateBestAnswer.aspx?QuestionId="+QuestionId;
                }
                if (QuestionCls.Type.ToLower().Equals("MultiChoice".ToLower()))
                {
                    UpdateUrl = "UpdateMultiChoice.aspx?QuestionId="+QuestionId;
                }
                if (QuestionCls.Type.ToLower().Equals("FreeText".ToLower()))
                {
                    UpdateUrl = "UpdateFreeText.aspx?QuestionId="+QuestionId;
                }

                if (QuestionCls.Type.ToLower().Equals("LargeFreeText".ToLower()))
                {
                    UpdateUrl = "UpdateFreeText.aspx?QuestionId="+QuestionId;
                }
                if (UpdateUrl.Trim().Length == 0)
                {
                    throw new Exception("Không cung cấp tính năng sửa chữa cho loại: " + QuestionCls.Type);
                }
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}